package com.vtuzx.kingdee.xqc_api.service.impl;

import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_core.dao.InterfaceHistoryDao;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 海神岛对接服务抽象父类
 */
public abstract class XqcServiceAbstract {

    /** 销售单接口调用履历 */
    @Autowired
    private InterfaceHistoryDao historyDao;

    /**
     * 成功日志
     *
     * @param _historyId 履历ID
     * @param param 入参
     * @param soap 返回值正文
     */
    protected void successLog(ObjectId _historyId, VtuzxMap param, String soap) {
        Document updateVal = new Document("error", false).append("errorMsg", "");
        if(!VtuzxUtil.isEmpty(soap)) updateVal.append("soap", soap);
        if (_historyId == null) {
            historyDao.insertOne(updateVal.append("param", param));
        } else {
            historyDao.updateById(_historyId, updateVal);
        }
    }

    /**
     * 错误日志
     *
     * @param _historyId 履历ID
     * @param param 入参
     * @param soap 返回值正文
     * @param error 错误消息
     */
    protected void errorLog(ObjectId _historyId, VtuzxMap param, String soap, String error) {
        Document updateVal = new Document("error", true);
        if(!VtuzxUtil.isEmpty(soap)) updateVal.append("soap", soap);
        if(!VtuzxUtil.isEmpty(error)) updateVal.append("errorMsg", error);
        if (_historyId == null) {
            historyDao.insertOne(updateVal.append("param", param).append("errorCnt", 1));
        } else {
            historyDao.updateByIdRedirect(_historyId,
                    new Document("$set", updateVal).append("$inc", new Document("errorCnt", 1)));
        }
    }
}
